﻿using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle;
using JshRCSWebApi.Infrastructure;
using JshRCSWebApi.Util.VehicleTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using JshRCSWebApi.Service.SysVehicle;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle.VehicleControl;
using Newtonsoft.Json;
using JshRCSWebApi.Infrastructure.Domain.DTO.SysVehicle.VehicleOther;
using JshRCSWebApi.Util;
using JshRCSWebApi.Infrastructure.Domain.VO;

namespace JshRCSWebApi.ServiceImpl.SysVehicle
{
    public class VehicleOtherServiceImpl:VehicleOtherService
    {
        private int vehiclePort = 19208;

        /// <summary>
        /// 顶升机构下降 6071   
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_other_jack_unload_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 6071;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 顶升机构停止 6072    
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_other_jack_stop_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 6072;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 顶升定高 6073    
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_other_jack_set_height_req(RequestDTO<robot_other_jack_set_height_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 6073;
            request.askJson = JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 顶升机构上升 6070     
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_other_jack_load_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 6070;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 开始扫描地图 6100      
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_other_slam_req(RequestDTO<robot_other_slam_DTO> requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 6100;
            request.askJson=JsonConvert.SerializeObject(requestDTO.askJson);
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

        /// <summary>
        /// 停止扫描地图 6101       
        /// </summary>
        /// <param name="requestDTO"></param>
        /// <returns></returns>
        public async Task<ApiResult> robot_other_endslam_req(RequestDTO requestDTO)
        {
            RequestDTO request = new RequestDTO();
            request.vehiclePort = vehiclePort;
            request.vehicleIP = requestDTO.vehicleIP;
            request.number = 6101;
            request.askJson = "";
            try
            {
                SeerTcpClient client = new SeerTcpClient(requestDTO.vehicleIP, request.vehiclePort);
                if (client.Connected)
                {
                    var str = client.SendseerMessage(request);
                    return await Task.Run(async () => ApiResult.Success("ok", JsonConvert.DeserializeObject<ResponseVO>(str)));
                }
                else
                    return await Task.Run(async () => ApiResult.Error("机器人网络连接断开，请检查网络"));
            }
            catch (SocketException)
            {
                return await Task.Run(async () => ApiResult.Error("SocketException"));
            }
            catch (IOException)
            {
                return await Task.Run(async () => ApiResult.Error("IOException"));
            }
        }

    }
}
